<template>
    <div>
        <div class="search-con search-con-top">
            <div>
                轮播图位置:
                <Select v-model="searchData.poisition" filterable style="width: 200px">
                    <Option :value="1">首页轮播图</Option>
                    <Option :value="2">陪驾页轮播图</Option>
                </Select>
                <Button @click="search" type="primary" icon="ios-search" style="margin-left: 10px;">搜索</Button>
                <Button @click="reset" type="warning" icon="ios-refresh" style="margin-left: 10px;">刷新</Button>
                <Button slot="extra" @click="edit(false)" type="success" style="margin-left: 10px;">添加</Button>
            </div>
        </div>
        <br>
        <Table :columns="columns" :data="list" @on-sort-change="customSort" :highlight-row="true" max-height="800" border
            stripe size="small">
            <template slot-scope="{ row , index}" slot="poisitionSlot">
                <span v-if="row.poisition == 1">首页轮播图</span>
                <span v-if="row.poisition == 2">陪驾页轮播图</span>
            </template>
            <template slot-scope="{ row , index}" slot="imgSlot">
                <show-img :imgUrl="row.img"></show-img>
            </template>
            <template slot-scope="{ row , index}" slot="typeSlot">
                <span v-if="row.type == 0">内部链接</span>
                <span v-if="row.type == 1">富文本</span>
            </template>
            <template slot-scope="{ row , index}" slot="contextSlot">
                <div style="width: 100px;height: 100px;" v-if="row.type == 1"><span v-html="row.content"></span></div>
                <div v-if="row.type == 0">{{ row.content }}</div>
            </template>
            <template slot-scope="{ row , index}" slot="makeSlot">
                <Tooltip content="编辑" style="margin-left: 10px;" placement="top">
                    <Icon type="ios-create" size="24" color="#2d8cf0" style="cursor: pointer;" @click="edit(row)" />
                </Tooltip>
                <Tooltip content="删除" style="margin-left: 10px;" placement="top">
                    <Icon type="md-trash" size="24" color="#ed4014" style="cursor: pointer;" @click.stop="del(row.id)" />
                </Tooltip>
            </template>
        </Table>
        <br>

        <Paginate :total="total" @on-change="changePage" @on-page-size-change="changeSize" :current="searchData.page">
        </Paginate>


        <Modal title="添加" width="70" v-model="showAdd" :mask-closable="false">
            <Form ref="formValidate" :model="formItem" :label-width="70">
                <FormItem label="位置">
                    <RadioGroup v-model="formItem.poisition">
                        <Radio :label="1" border>首页轮播图</Radio>
                        <Radio :label="2" border>陪驾页轮播图</Radio>
                    </RadioGroup>
                </FormItem>
                <FormItem label="图片">
                    <MyUpload :maxNumber="1" @getImages="getImg" :defaultImags="[formItem.img]" :imageSize="120"></MyUpload>
                </FormItem>
                <FormItem label="跳转类型">
                    <RadioGroup v-model="formItem.type">
                        <Radio :label="0" border>内部链接</Radio>
                        <Radio :label="1" border>富文本</Radio>
                    </RadioGroup>
                </FormItem>
                <FormItem label="跳转地址" v-show="formItem.type == 0">
                    <Input v-model="formItem.content"></Input>
                </FormItem>
                <FormItem label="内容" v-show="formItem.type == 1">
                    <RichText ref="ric" @getEditorContent="getRichTextContent" :content="formItem.content"></RichText>
                </FormItem>
                <FormItem label="排序值">
                    <Input v-model="formItem.sort" type="number"></Input>
                    <Alert type="info">排序值从大到小排序</Alert>
                </FormItem>
            </Form>
            <div slot="footer">
                <Button type="primary" size="large" long @click="addQuestion">确定</Button>
            </div>
        </Modal>
    </div>
</template>

<script>
import Paginate from "_c/paginate";
import RichText from "_c/rich-text";
import MyUpload from "_c/my-upload";
import ShowImg from '_c/show_img'
export default {
    name: "banner",
    components: {
        RichText, Paginate, MyUpload, ShowImg
    },
    data() {
        return {
            searchData: {
                page: 1,
                limit: 10
            },
            // 列表
            columns: [
                {
                    title: '位置',
                    key: 'min',
                    align: 'center',
                    slot: "poisitionSlot"
                },
                {
                    title: '图片',
                    key: 'img',
                    align: "center",
                    slot: "imgSlot"
                },
                {
                    title: '跳转类型',
                    key: 'min',
                    align: 'center',
                    slot: "typeSlot"
                },
                {
                    title: '内容',
                    key: 'max',
                    align: 'center',
                    slot: "contextSlot"
                },
                {
                    title: '排序值',
                    key: 'sort',
                    align: 'center',
                },
                {
                    title: '创建时间',
                    key: 'create_time',
                    align: 'center',
                },
                {
                    title: '操作',
                    align: 'center',
                    slot: 'makeSlot',
                },
            ],
            // 数据
            list: [],
            total: 0,
            formItem: {},
            showAdd: false,
        }
    },
    methods: {
        getImg(items) {
            items.forEach(item => {
                this.formItem.img = item.url;
            });
        },
        getRichTextContent(value) {
            this.formItem.content = value;
        },
        // 查询管理员信息
        getList() {
            this.$axios('/admin/banner/getList', this.searchData).then(res => {
                this.list = res.data;
                this.total = res.total;
            });
        },
        search() {
            this.searchData.page = 1;
            this.getList();
        },
        reset() {
            this.searchData = {
                page: 1,
                limit: 10,
            };
            this.getList();
        },
        changePage(page) {
            this.searchData.page = page;
            this.getList();
        },
        changeSize(size) {
            this.searchData.limit = size;
            this.getList();
        },
        customSort(column, key, order) {
            this.searchData.order_by = column.key;
            this.searchData.sort = column.order;
            this.getList();
        },
        addQuestion() {
            this.$axios('/admin/banner/save', this.formItem).then(res => {
                this.$Message.success('保存成功');
                this.formItem = {};
                this.showAdd = false;
                this.getList();
            });

        },
        edit(row) {
            if (row) {
                this.formItem = { ...row };
            } else {
                this.formItem = {
                    id: 0,
                    img: '',
                    type: 0,
                    content: '',
                    poisition: 1
                }
            }
            this.showAdd = true;
            this.$nextTick(() => {
                this.$refs.ric.setContent(this.formItem.content);
            });
        },
        del(id) {
            this.$axios('/admin/banner/del', { id: id }).then(res => {
                this.$Message.success('删除成功');
                this.getList();
            });
        },

    },
    created() {
        this.getList();
    }
}
</script>
